package com.ruoyi.web.controller.work;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.work.domain.WorkMediaFlow;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.work.domain.WorkMediaNet;
import com.ruoyi.work.service.IWorkMediaNetService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 网络媒体资料Controller
 * 
 * @author ruoyi
 * @date 2022-01-02
 */
@Controller
@RequestMapping("/work/net")
public class WorkMediaNetController extends BaseController
{
    private String prefix = "work/net";

    @Autowired
    private IWorkMediaNetService workMediaNetService;

    @RequiresPermissions("work:net:view")
    @GetMapping()
    public String net()
    {
        return prefix + "/net";
    }

    /**
     * 查询网络媒体资料列表
     */
    @RequiresPermissions("work:net:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WorkMediaNet workMediaNet)
    {
        startPage();
        List<WorkMediaNet> list = workMediaNetService.selectWorkMediaNetList(workMediaNet);
        return getDataTable(list);
    }

    /**
     * 导出网络媒体资料列表
     */
    @RequiresPermissions("work:net:export")
    @Log(title = "网络媒体资料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WorkMediaNet workMediaNet)
    {
        List<WorkMediaNet> list = workMediaNetService.selectWorkMediaNetList(workMediaNet);
        ExcelUtil<WorkMediaNet> util = new ExcelUtil<WorkMediaNet>(WorkMediaNet.class);
        return util.exportExcel(list, "网络媒体资料数据");
    }

    /**
     * 新增网络媒体资料
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存网络媒体资料
     */
    @RequiresPermissions("work:net:add")
    @Log(title = "网络媒体资料", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WorkMediaNet workMediaNet)
    {
        workMediaNet.setCreateBy(getLoginName());
        return toAjax(workMediaNetService.insertWorkMediaNet(workMediaNet));
    }

    /**
     * 修改网络媒体资料
     */
    @RequiresPermissions("work:net:edit")
    @GetMapping("/edit/{mediaId}")
    public String edit(@PathVariable("mediaId") Long mediaId, ModelMap mmap)
    {
        WorkMediaNet workMediaNet = workMediaNetService.selectWorkMediaNetByMediaId((mediaId));
        mmap.put("workMediaNet", workMediaNet);
        return prefix + "/edit";
    }

    /**
     * 修改保存网络媒体资料
     */
    @RequiresPermissions("work:net:edit")
    @Log(title = "网络媒体资料", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WorkMediaNet workMediaNet)
    {
        return toAjax(workMediaNetService.updateWorkMediaNet(workMediaNet));
    }

    /**
     * 删除网络媒体资料
     */
    @RequiresPermissions("work:net:remove")
    @Log(title = "网络媒体资料", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(workMediaNetService.deleteWorkMediaNetByMediaIds(ids));
    }
    /**
     *
     * 下载模板
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<WorkMediaNet> util = new ExcelUtil<WorkMediaNet>(WorkMediaNet.class);
        return util.importTemplateExcel("网络媒体模板");
    }
    /**
     * 导入数据
     */
    @RequiresPermissions("work:net:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<WorkMediaNet> util = new ExcelUtil<WorkMediaNet>(WorkMediaNet.class);
        List<WorkMediaNet> mediaList = util.importExcel(file.getInputStream());
        String message = importPaper(mediaList, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 导入APP媒体数据
     *
     * @param mediaList 稿件数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @return 结果
     */
    public String importPaper(List<WorkMediaNet> mediaList, Boolean isUpdateSupport)
    {
        if (StringUtils.isNull(mediaList) || mediaList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (WorkMediaNet media : mediaList)
        {
            try
            {

                //TODO 无此媒体，新增
                if(StringUtils.isEmpty(media.getMediaName())){
                    throw new Exception("媒体名称不能为空");
                }
                if(StringUtils.isNull(media.getPrice())){
                    throw new Exception("价格不能为空");
                }

                if(StringUtils.isNull(media.getContactId())){
                    throw new Exception("联系人不能为空");
                }
                media.setCreateBy(getLoginName());
                workMediaNetService.insertWorkMediaNet(media);
                successNum++;
                successMsg.append("<br/>" + successNum + "、媒体名称：" + media.getMediaName() + " 导入成功");

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、媒体名称 " +  media.getMediaName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
